//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Workflow.ComponentModel;
using System.Collections;
using System.Collections.Specialized;
using System.Workflow.ComponentModel.Design;
using System.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Data.Odbc;
using System.Xml;
using Microsoft.Research.ScientificWorkflow;
using System.IO;
using Microsoft.AnalysisServices.Extensions;

namespace Microsoft.Research.ScientificWorkflow.Activities
{
    [Name("OLAP Cube generator")]
    [Description("Activity to generate an OLAP cube.")]
    [WorkflowCategory("Database")]
    class OLAPCubeGenerator : Activity
    {
        #region Dependency Properties

        public static DependencyProperty DBServerNameProperty = DependencyProperty.Register("DBServerName",
            typeof(string), typeof(OLAPCubeGenerator));

        [RequiredInputParam]
        [Name("Database Server")]
        [Description("Name of the database server.")]
        public string DBServerName
        {
            get
            {
                return ((string)(base.GetValue(OLAPCubeGenerator.DBServerNameProperty)));
            }
            set
            {
                base.SetValue(OLAPCubeGenerator.DBServerNameProperty, value);
            }
        }

        public static DependencyProperty DBNameProperty = DependencyProperty.Register("DBName", 
            typeof(string), typeof(OLAPCubeGenerator));

        [RequiredInputParam]
        [Name("Database Instance Name")]
        [Description("Name of the database instance for getting the data.")]
        public string DBName
        {
            get
            {
                return ((string)(base.GetValue(OLAPCubeGenerator.DBNameProperty)));
            }
            set
            {
                base.SetValue(OLAPCubeGenerator.DBNameProperty, value);
            }
        }

        public static DependencyProperty AnalysisServerNameProperty = DependencyProperty.Register("AnalysisServerName", 
            typeof(string), typeof(OLAPCubeGenerator));

        [RequiredInputParam]
        [Name("AnalysisServerName")]
        [Description("Name of the analysis server.")]
        public string AnalysisServerName
        {
            get
            {
                return ((string)(base.GetValue(OLAPCubeGenerator.AnalysisServerNameProperty)));
            }
            set
            {
                base.SetValue(OLAPCubeGenerator.AnalysisServerNameProperty, value);
            }
        }

        public static DependencyProperty DimensionTablesProperty = DependencyProperty.Register("DimensionTables",
            typeof(string), typeof(OLAPCubeGenerator));

        [RequiredInputParam]
        [Name("Dimension Table Names.")]
        [Description("Comma seperated names of the dimension tables.")]
        public string DimensionTables
        {
            get
            {
                return ((string)(base.GetValue(OLAPCubeGenerator.DimensionTablesProperty)));
            }
            set
            {
                base.SetValue(OLAPCubeGenerator.DimensionTablesProperty, value);
            }
        }

        public static DependencyProperty FactTableNameProperty = DependencyProperty.Register("FactTableName", 
            typeof(string), typeof(OLAPCubeGenerator));

        [RequiredInputParam]
        [Name("Fact Table Name.")]
        [Description("Name of the table which contains data to be measured.")]
        public string FactTableName
        {
            get
            {
                return ((string)(base.GetValue(OLAPCubeGenerator.FactTableNameProperty)));
            }
            set
            {
                base.SetValue(OLAPCubeGenerator.FactTableNameProperty, value);
            }
        }

        public static DependencyProperty CubeNameProperty = DependencyProperty.Register("CubeName", 
            typeof(string), typeof(OLAPCubeGenerator));

        [RequiredInputParam]
        [Name("Cube Name")]
        [Description("Name to be assigned to the generated cube.")]
        public string CubeName
        {
            get
            {
                return ((string)(base.GetValue(OLAPCubeGenerator.CubeNameProperty)));
            }
            set
            {
                base.SetValue(OLAPCubeGenerator.CubeNameProperty, value);
            }
        }

        public static DependencyProperty MeasureColumnsProperty = DependencyProperty.Register("MeasureColumns", 
            typeof(string), typeof(OLAPCubeGenerator));

        [RequiredInputParam]
        [Name("Measure Columns")]
        [Description("Name of the columns to be used for measuring.")]
        public string MeasureColumns
        {
            get
            {
                return ((string)(base.GetValue(OLAPCubeGenerator.MeasureColumnsProperty)));
            }
            set
            {
                base.SetValue(OLAPCubeGenerator.MeasureColumnsProperty, value);
            }
        }

        #endregion

        #region "Override functions"

        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            if (null == executionContext)
                throw new ArgumentNullException("executionContext");
            try
            {
                Console.WriteLine("Generating Cube-->" + this.CubeName + "...");
                SQLServerCubeGenerator objSqlGen = new SQLServerCubeGenerator();

                objSqlGen.DataSourceName = this.DBServerName;
                objSqlGen.OlapDBName = this.AnalysisServerName;
                objSqlGen.SQLDBName = this.DBName;
                objSqlGen.FactTableName = this.FactTableName;
                objSqlGen.DimensionTableName = this.DimensionTables;
                objSqlGen.MeasureColumns = this.MeasureColumns;
                objSqlGen.CubeName = this.CubeName;

                objSqlGen.GenerateCube();
                Console.WriteLine("Cube Generated");
                return ActivityExecutionStatus.Closed;

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine("Cube generate Process Failed");
                return ActivityExecutionStatus.Faulting;
            }

        }

        #endregion
    }
}
